package cc.vicp.djx314.starfood.util.impls;

import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

import org.springframework.util.Assert;

import cc.vicp.djx314.starfood.domain.dish.ProvideTime;
import cc.vicp.djx314.starfood.domain.dish.SeriesDishProvideTime;
import cc.vicp.djx314.starfood.util.DaysOfWeekTran;
import cc.vicp.djx314.starfood.util.ModelTreatment;

/**
 * ModelTreatmentInter的实现类
 * @author 水山清风
 *
 */
public class ModelTreatmentImpl implements ModelTreatment {
	/**
	 * 星期数和数字转换对象
	 */
	private DaysOfWeekTran daysOfWeekTran;
	/**
	 * 设置daysOfWeekTranInter
	 * @param daysOfWeekTranInter
	 */
	public void setDaysOfWeekTran(DaysOfWeekTran daysOfWeekTran) {
		this.daysOfWeekTran = daysOfWeekTran;
	}

	public static final String MONDAY_TO_FRIDAY = "周一、周二、周三、周四、周五、";
	public static final String MON_FRI = "周一到周五、";
	public static final String MONDAY_TO_SUNDAY = "周一、周二、周三、周四、周五、周六、周日、";
	public static final String EVERYDAY = "每天、";

	public String simpleReplace(String weekInfo) {
		weekInfo = weekInfo.replaceAll(ModelTreatmentImpl.MONDAY_TO_SUNDAY, ModelTreatmentImpl.EVERYDAY);
		weekInfo = weekInfo.replaceAll(ModelTreatmentImpl.MONDAY_TO_FRIDAY, ModelTreatmentImpl.MON_FRI);
		weekInfo = weekInfo.substring(0, weekInfo.length() - 1);
		return weekInfo;
	}

	public List<SeriesDishProvideTime> setSeriesDishProvideTimes(
			final List<ProvideTime> provideTimes) throws Throwable {
		List<SeriesDishProvideTime> result = new ArrayList<SeriesDishProvideTime>();
		SeriesDishProvideTime seriesDishProvideTime = null;
		Time startTime = null;
		Time endTime = null;
		String weekInfo = new String("");
		int i = 0;
		for (ProvideTime provideTime : provideTimes) {
			Assert.notNull(provideTime.getDayOfWeek(),
					"com.model.dishRelated.ProvideTime没有属性dayOfWeek");
			Assert.notNull(provideTime.getStartTime(),
					"com.model.dishRelated.ProvideTime没有属性startTime");
			Assert.notNull(provideTime.getEndTime(),
					"com.model.dishRelated.ProvideTime没有属性endTime");
			if (i == 0 && provideTimes.size() > 1) {
				startTime = provideTime.getStartTime();
				endTime = provideTime.getEndTime();
				weekInfo += "周" + daysOfWeekTran.getWeekDay(provideTime.getDayOfWeek()) + "、";
			} else if (i == 0 && provideTimes.size() == 1) {
				seriesDishProvideTime = new SeriesDishProvideTime(
						"周" + daysOfWeekTran.getWeekDay(provideTime.getDayOfWeek()),
						provideTime.getStartTime(),
						provideTime.getEndTime());

				result.add(seriesDishProvideTime);
			} else if (i < provideTimes.size() - 1) {
				if (startTime.equals(provideTime.getStartTime()) &&
						endTime.equals(provideTime.getEndTime())) {
					weekInfo += "周" + daysOfWeekTran.getWeekDay(provideTime.getDayOfWeek()) + "、";
				} else {
					weekInfo = simpleReplace(weekInfo);

					seriesDishProvideTime = new SeriesDishProvideTime(weekInfo, startTime, endTime);
					result.add(seriesDishProvideTime);

					weekInfo = "周" + daysOfWeekTran.getWeekDay(provideTime.getDayOfWeek()) + "、";
					startTime = provideTime.getStartTime();
					endTime = provideTime.getEndTime();
				}
			} else {
				if (startTime.equals(provideTime.getStartTime()) &&
						endTime.equals(provideTime.getEndTime())) {
					weekInfo += "周" + daysOfWeekTran.getWeekDay(provideTime.getDayOfWeek()) + "、";
				} else {
					weekInfo = simpleReplace(weekInfo);

					seriesDishProvideTime = new SeriesDishProvideTime(weekInfo, startTime, endTime);
					result.add(seriesDishProvideTime);

					weekInfo = "周" + daysOfWeekTran.getWeekDay(provideTime.getDayOfWeek()) + "、";
				}

				weekInfo = simpleReplace(weekInfo);
				seriesDishProvideTime = new SeriesDishProvideTime(weekInfo,
						provideTime.getStartTime(),
						provideTime.getEndTime());
				result.add(seriesDishProvideTime);
			}
			i += 1;
		}
		Integer j = 0;
		for (SeriesDishProvideTime series : result) {
			j = j + 1;
			series.setId(j);
		}
		return result;
	}
}